package com.lazy.app.ui.act;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.imageview.ShapeableImageView;
import com.lazy.app.R;
import com.lazy.app.databinding.ActivityMainBinding;
import com.lazy.app.presenter.MainPresenter;
import com.lazy.app.ui.fragment.TestFragment;
import com.zndroid.base.ui.common.CommonNavigationTabActivity;
import com.zndroid.navigation.NavigationTabBarVP2;
import com.zndroid.utils.XUtils;

public class MainActivity extends CommonNavigationTabActivity<ActivityMainBinding, MainPresenter> {
    @Override
    protected NavigationTabBarVP2 onSupplyNavigationTabView() {
        return currentViewBinding().main.ntbTabs;
    }

    @Override
    protected NavigationTabBarVP2.Model[] onSupplyBars() {
        NavigationTabBarVP2.Model model0 = new NavigationTabBarVP2.Model.Builder(ResourcesCompat.getDrawable(getResources(), R.mipmap.temp, null), Color.parseColor("#F7F7F7"))
                .title("发现")
                .selectedIcon(ResourcesCompat.getDrawable(getResources(), R.mipmap.temp, getTheme()))
                .build();
        NavigationTabBarVP2.Model model1 = new NavigationTabBarVP2.Model.Builder(ResourcesCompat.getDrawable(getResources(), R.mipmap.temp, null), Color.parseColor("#F7F7F7"))
                .title("社区")
                .selectedIcon(ResourcesCompat.getDrawable(getResources(), R.mipmap.temp, getTheme()))
                .build();
        NavigationTabBarVP2.Model model2 = new NavigationTabBarVP2.Model.Builder(ResourcesCompat.getDrawable(getResources(), R.mipmap.temp, null), Color.parseColor("#F7F7F7"))
                .title("我的")
                .selectedIcon(ResourcesCompat.getDrawable(getResources(), R.mipmap.temp, getTheme()))
                .build();

        return new NavigationTabBarVP2.Model[] {
                model0, model1, model2
        };
    }

    @Override
    protected boolean isBehaviorEnabled() {
        return true;
    }

    @Override
    protected ViewPager2 onSupplyViewPage2() {
        return currentViewBinding().main.vpMain;
    }

    @Override
    protected Fragment[] onSupplyFragments() {
        TestFragment f0 = new TestFragment();
        Bundle b0 = new Bundle();
        b0.putString(TestFragment.KEY, "要闻xxx");
        f0.setArguments(b0);

        TestFragment f1 = new TestFragment();
        Bundle b1 = new Bundle();
        b1.putString(TestFragment.KEY, "我的xxx");
        f1.setArguments(b1);

        TestFragment f2 = new TestFragment();
        Bundle b2 = new Bundle();
        b2.putString(TestFragment.KEY, "设置xxx");
        f2.setArguments(b2);

        return new Fragment[] {
                f0, f1, f2
        };
    }

    @Override
    protected ActivityMainBinding onSupplyViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityMainBinding.inflate(layoutInflater);
    }

    @Override
    protected MainPresenter onSupplyPresenter() {
        return new MainPresenter();
    }

    @Override
    protected Toolbar onSupplyToolBar() {
        return currentViewBinding().main.toolbar;
    }

    @Override
    public void onBackPressed() {
        if (currentViewBinding().drawerLayout.isDrawerOpen(GravityCompat.START)) {
            currentViewBinding().drawerLayout.closeDrawers();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void doOnCreate(@Nullable Bundle savedInstanceState) {
        super.doOnCreate(savedInstanceState);

        DrawerLayout drawerLayout = currentViewBinding().drawerLayout;
        MaterialToolbar toolbar = currentViewBinding().main.toolbar;

        //隐藏自带的标题
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        ShapeableImageView headImageView = toolbar.findViewById(R.id.siv_head);
        headImageView.setOnClickListener(v -> {
            drawerLayout.openDrawer(GravityCompat.START, true);
        });

        AppCompatTextView titleTextView = toolbar.findViewById(R.id.tv_title);
        AppCompatTextView nickNameTextView = toolbar.findViewById(R.id.tv_nick_name);

        nickNameTextView.setText("Kitty");
        titleTextView.setText("大美女");

        drawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                currentViewBinding().drawerLeftMenu.menuLayout.btnSettings.setOnClickListener(v -> {
                    toActivityCallBackAble(new Intent(MainActivity.this, SettingsActivity.class));
                });

                currentViewBinding().drawerLeftMenu.menuLayout.btnAbout.setOnClickListener(v -> {
                    toActivityCallBackAble(new Intent(MainActivity.this, AboutActivity.class));
                });
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                super.onDrawerStateChanged(newState);
            }
        });
    }

    @Override
    protected void doOnResume() {
        super.doOnResume();
        XUtils.create().screenUtil().setStatusBarGradientColor(this, currentViewBinding().main.toolbar);
    }

    public void test(View view) {
        jumpTo(LoginActivity.class);
    }

    public void test1(View view) {
        jumpTo(RegisterActivity.class);
    }
}
